package gui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import util.Parser;
import util.Protocol;
import net.BackClient;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LogInUI extends JFrame 
{
    private static final long serialVersionUID = 7639002619638253931L;
    
    final int num = 521;
    private JPanel contentPane;
    
    private JTextField userField;
    
    private JPasswordField passwordField;
    
    byte[] sentence = new byte[num];
    
    BackClient backClient = new BackClient();

    Protocol protocol = new Protocol();
    
    Parser parser = new Parser(); 
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                try
                {
                    LogInUI frame = new LogInUI();
                    frame.setVisible(true);
                    frame.setResizable(false);
                } catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LogInUI() 
    {
        setTitle("登录");
        getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 304, 162);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel label = new JLabel("账号：");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(20, 29, 51, 24);
        contentPane.add(label);
        
        JLabel label_1 = new JLabel("密码：");
        label_1.setFont(new Font("宋体", Font.PLAIN, 17));
        label_1.setBounds(20, 63, 56, 25);
        contentPane.add(label_1);
        
        userField = new JTextField();
        userField.setBounds(81, 32, 78, 21);
        contentPane.add(userField);
        userField.setColumns(10);
        
        passwordField = new JPasswordField();
        passwordField.setBounds(81, 67, 78, 21);
        contentPane.add(passwordField);
        
        JButton button = new JButton("登录");
        button.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                String nickname = userField.getText();
                String password = String.valueOf(passwordField.getPassword());
                String information = nickname + "," + password;
                
                String massage = protocol.getLogInInformation(information);
                backClient.SendMassage(massage);
                String recieve = backClient.ReseiveMassage();
                
                System.out.println(recieve);
                String [] temp = recieve.split(";");
                String head = temp[0];
                boolean issuccess = parser.isLogin(head);
                if(issuccess)
                {
                    String friendID = temp[1];
                    LogInUI.this.setVisible(false);;
                    FriendList ui = new FriendList();
                    ui.setVisible(true);
                    ui.setFirendID(friendID);
                    ui.setNickname(nickname);
                }
                else{
                    JOptionPane.showMessageDialog(null, "账号密码错误");
                }
            }
        });
        button.setBounds(187, 65, 95, 25);
        contentPane.add(button);
        
        JLabel lblNewLabel = new JLabel("登录");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel.setBounds(122, 7, 62, 15);
        contentPane.add(lblNewLabel);
        
        JButton button_1 = new JButton("注册");
        button_1.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                RegisterUI registerUI = new RegisterUI();
                registerUI.setVisible(true);
                LogInUI.this.setVisible(false);
            }
        });
        button_1.setBounds(187, 30, 95, 25);
        contentPane.add(button_1);
    }
}
